<?php

function home_student() {
	global $smarty;
	return $smarty->fetch("home_student.html");
}

function process_student() {
	global $smarty, $function;
	$a = isset($_GET['a']) ? $_GET['a'] : "";
	$oStudent = new Student();
	$oTutor = new Tutor();
	
	switch(strtolower($a)) {
	
		case "choosetest":
			$list = $oTutor->showAllReadyTests();
			$smarty->assign("list_ready_tests", $list);
			return $smarty->fetch("student/choosetest.html");
		break;
		
		case "assessment":
			return $smarty->fetch("student/assessments.html");
		break;
		
		case "aunit":
			/*$list_unit_assessment = $oTutor->showAllReadyTestsInUnit();
			$smarty->assign("unit", 1); // use it to know unit or module, 1 = unit, 0 = module
			$smarty->assign("list_assessments", $list_unit_assessment);
			return $smarty->fetch("student/list_assessments.html");*/
			$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
			if($id !=0) {
				$list_unit_assessment = $oTutor->showAllReadyTestsInUnitID($id);
				$smarty->assign("unit", 1); // use it to know unit or module, 1 = unit, 0 = module
				$smarty->assign("list_assessments", $list_unit_assessment);
				return $smarty->fetch("student/list_assessments.html");
			} else {
				$list_unit = $oStudent->showAllUnits();
				$smarty->assign("list_unit", $list_unit);
				return $smarty->fetch("student/list_unit.html");
			}
		break;
		
		case "amodule":
			$list_module_assessment = $oTutor->showAllReadyTestsInModule();
			$smarty->assign("unit", 0); // use it to know unit or module, 1 = unit, 0 = module
			$smarty->assign("list_assessments", $list_module_assessment);
			return $smarty->fetch("student/list_assessments.html");
		break;
		
		/*case "reviewtest":
			if(isset($_POST['btn_submit'])) {
				$number_questions = intval($_POST['number_questions']);
				$testid = intval($_POST['testid']);
				$questionid = $_POST['qid'];
				$question_model = $_POST['q_model'];
				$op1 = $op2 = $op3 = $op4 = $op5;
				for($i=1; $i <= $number_questions; $i++) {
					$a_name = "answer".$i;
					$answer[$i] = $_POST[$a_name];
					if($answer[$i] != "") {
						if($question_model[$i-1] == 0) { // 0: radio
							$nanswer[$i] = explode("|", $answer[$i]);
							switch(intval($nanswer[$i][0])) {
								case 1:
									$where_clause = " AND SystemAnswerOption1='Y'";
									$op1 = $nanswer[$i][1];
								break;
								case 2:
									$where_clause = " AND SystemAnswerOption2='Y'";
									$op2 = $nanswer[$i][1];
								break;
								case 3:
									$where_clause = " AND SystemAnswerOption3='Y'";
									$op3 = $nanswer[$i][1];
								break;
								case 4:
									$where_clause = " AND SystemAnswerOption4='Y'";
									$op4 = $nanswer[$i][1];
								break;
								case 5:
									$where_clause = " AND SystemAnswerOption5='Y'";
									$op5 = $nanswer[$i][1];
								break;
							}
							$mark = intval($oStudent->do_check_answer_and_get_mark($testid, $questionid[$i-1], $where_clause));
						} elseif($question_model[$i-1] == 1) { // 1: multi
							for($j=0; $j < count($answer[$i]); $j++) {
								$nanswer[$i][$j] = explode("|", $answer[$i][$j]);
								switch(intval($nanswer[$i][$j][0])) {
									case 1:
										$where_clause1 = " AND SystemAnswerOption1='Y'";
										$op1 = $nanswer[$i][$j][1];
									break;
									case 2:
										$where_clause2 = " AND SystemAnswerOption2='Y'";
										$op2 = $nanswer[$i][$j][1];
									break;
									case 3:
										$where_clause3 = " AND SystemAnswerOption3='Y'";
										$op3 = $nanswer[$i][$j][1];
									break;
									case 4:
										$where_clause4 = " AND SystemAnswerOption4='Y'";
										$op4 = $nanswer[$i][$j][1];
									break;
									case 5:
										$where_clause5 = " AND SystemAnswerOption5='Y'";
										$op5 = $nanswer[$i][$j][1];
									break;
								}
							}
							$where_clause = $where_clause1.$where_clause2.$where_clause3.$where_clause4.$where_clause5;
							$mark = intval($oStudent->do_check_answer_and_get_mark($testid, $questionid[$i-1], $where_clause));
						} elseif($question_model[$i-1] == 2) { // 2: 1 word answer
							// do check answer
							$nanswer[$i] = explode(" ", $answer[$i]);
							$op1 = strtoupper($nanswer[$i][0]);
							$op2 = $op3 = $op4 = $op5 = "";
							$where_clause = " AND SystemAnswerOption1 LIKE '".$op1."'";
							$mark = intval($oStudent->do_check_answer_and_get_mark($testid, $questionid[$i-1], $where_clause));
						} else { // 3: textarea answer
							// tutor do check answer
							$answer[$i] = stripslashes($function->FixQuotes($answer[$i]));
							$op1 = $op2 = $op3 = $op4 = $op5 = "";
							$mark = 0;
						}
						$oStudent->answerQuestionsAtSecond($testid, $questionid[$i - 1], $_SESSION['user_logged']['UID'], $answer[$i], $mark, $op1, $op2, $op3, $op4, $op5);
					}
				}	
				return $function->msg_box("You have done a Review Test. Thank you very much.",2,"?mod=student&a=choosetest");
			} else {
				$testid = intval($_GET['test']);
				$check_do_test = $oStudent->check_do_test($testid, $_SESSION['user_logged']['UID']);
				if($check_do_test === false) {
					return $function->msg_box("You haven't done this test. Please choose Do Test to do your answers.",2,"?mod=student&a=choosetest");
				} else {
					$list_questions = $oStudent->getAllQuestionsAnswerInTest($testid, $_SESSION['user_logged']['UID']);
					$number_questions = count($list_questions);
					//$smarty->assign("attempt", $done_test['Attempt']);
					$smarty->assign("number_questions", $number_questions);
					$smarty->assign("testid", $testid);
					$smarty->assign("list_questions", $list_questions);
					return $smarty->fetch("student/reviewtest.html");
				}
			}
		break;*/
		
		case "dotest":
			if(isset($_POST['btn_submit'])) {
				$number_questions = intval($_POST['number_questions']);
				$testid = intval($_POST['testid']);
				$check_test_module_submit = $oStudent->check_module_test($testid, $_SESSION['user_logged']['UID']); // test of module just can do one time
				if($check_test_module_submit) {
					return $function->msg_box("You had done this test. Please choose Review Done Assessment to review your answers.",2,"?mod=student&a=amodule");
				}
				$attempt = intval($_POST['attempt']);
				$new_attempt = $attempt + 1;
				$check_test_attempt = $oStudent->check_test_attempt($testid, $_SESSION['user_logged']['UID'], $new_attempt);
				if($check_test_attempt) {
					return $function->msg_box("You had done this test with this attempt. Please try a new attempt.",2,"?mod=student&a=aunit");
				}
				$questionid = $_POST['qid'];
				$question_model = $_POST['q_model'];
				$op1 = $op2 = $op3 = $op4 = $op5;
				for($i=1; $i <= $number_questions; $i++) {
					$a_name = "answer".$i;
					$answer[$i] = $_POST[$a_name];
					if($answer[$i] != "") {
						if($question_model[$i-1] == 0) { // 0: radio
							$nanswer[$i] = explode("|", $answer[$i]);
							// bao loi maximum 30 seconds exceeds
							/*for($k = 1; $k <= 5; $k++) {
								if($nanswer[$i][0] == $k) {
									//do check answer and get mark
									$where_clause = " AND SystemAnswerOption".$k."='Y'";
									$mark = intval($oStudent->do_check_answer_and_get_mark($testid, $questionid[$i-1], $where_clause));
									$op.$k = $nanswer[$i][1];
									//echo $op.$k;exit;
								} else $op.$k = "";
							}*/
							switch(intval($nanswer[$i][0])) {
								case 1:
									$where_clause = " AND SystemAnswerOption1='Y'";
									$op1 = $nanswer[$i][1];
								break;
								case 2:
									$where_clause = " AND SystemAnswerOption2='Y'";
									$op2 = $nanswer[$i][1];
								break;
								case 3:
									$where_clause = " AND SystemAnswerOption3='Y'";
									$op3 = $nanswer[$i][1];
								break;
								case 4:
									$where_clause = " AND SystemAnswerOption4='Y'";
									$op4 = $nanswer[$i][1];
								break;
								case 5:
									$where_clause = " AND SystemAnswerOption5='Y'";
									$op5 = $nanswer[$i][1];
								break;
							}
							$mark = intval($oStudent->do_check_answer_and_get_mark($testid, $questionid[$i-1], $where_clause));
						} elseif($question_model[$i-1] == 1) { // 1: multi
							for($j=0; $j < count($answer[$i]); $j++) {
								$nanswer[$i][$j] = explode("|", $answer[$i][$j]);
								switch(intval($nanswer[$i][$j][0])) {
									case 1:
										$where_clause1 = " AND SystemAnswerOption1='Y'";
										$op1 = $nanswer[$i][$j][1];
									break;
									case 2:
										$where_clause2 = " AND SystemAnswerOption2='Y'";
										$op2 = $nanswer[$i][$j][1];
									break;
									case 3:
										$where_clause3 = " AND SystemAnswerOption3='Y'";
										$op3 = $nanswer[$i][$j][1];
									break;
									case 4:
										$where_clause4 = " AND SystemAnswerOption4='Y'";
										$op4 = $nanswer[$i][$j][1];
									break;
									case 5:
										$where_clause5 = " AND SystemAnswerOption5='Y'";
										$op5 = $nanswer[$i][$j][1];
									break;
								}
							}
							$where_clause = $where_clause1.$where_clause2.$where_clause3.$where_clause4.$where_clause5;
							$mark = intval($oStudent->do_check_answer_and_get_mark($testid, $questionid[$i-1], $where_clause));
						} elseif($question_model[$i-1] == 2) { // 2: 1 word answer
							// do check answer
							$nanswer[$i] = explode(" ", $answer[$i]);
							$op1 = strtoupper($nanswer[$i][0]);
							$op2 = $op3 = $op4 = $op5 = "";
							$where_clause = " AND SystemAnswerOption1 LIKE '".$op1."'";
							$mark = intval($oStudent->do_check_answer_and_get_mark($testid, $questionid[$i-1], $where_clause));
						} else { // 3: textarea answer
							// tutor do check answer
							$answer[$i] = stripslashes($function->FixQuotes($answer[$i]));
							$op1 = $op2 = $op3 = $op4 = $op5 = "";
							$mark = 0;
						}
						// insert test answer
						//$check = $oStudent->checkAnswerExist($testid, $questionid[$i - 1], $_SESSION['user_logged']['UID']);
						//if($check === true) {
						//	$good = $oStudent->answerQuestionsAtSecond($testid, $questionid[$i - 1], $_SESSION['user_logged']['UID'], $answer[$i], $mark, $op1, $op2, $op3, $op4, $op5, $new_attempt);
						//} else {
							$good = $oStudent->answerQuestionsAtFirst($testid, $questionid[$i - 1], $_SESSION['user_logged']['UID'], $answer[$i], $mark, $op1, $op2, $op3, $op4, $op5, $new_attempt);
						//}
					} else {
						// answer is empty
						$mark = 0;
						//$check = $oStudent->checkAnswerExist($testid, $questionid[$i - 1], $_SESSION['user_logged']['UID']);
						//if($check === true) {
						//	$good = $oStudent->answerQuestionsAtSecond($testid, $questionid[$i - 1], $_SESSION['user_logged']['UID'], $answer[$i], $mark, $op1, $op2, $op3, $op4, $op5, $new_attempt);
						//} else {
							$good = $oStudent->answerQuestionsAtFirst($testid, $questionid[$i - 1], $_SESSION['user_logged']['UID'], $answer[$i], $mark, $op1, $op2, $op3, $op4, $op5, $new_attempt);
						//}
					}
				}
				//-------- get sum marks of a attempt------------
				$overall_mark = $oStudent->sumMarks($testid, $_SESSION['user_logged']['UID'], $new_attempt);
				//-----------------------------------------------
				//----------- add test attempts -----------------
				$oStudent->addTestAttempts($testid, $_SESSION['user_logged']['UID'], $overall_mark, $new_attempt);
				//-----------------------------------------------
				if($good) {
					return $function->msg_box("You have done a Test. Thank you very much.",2,"?mod=hstudent");
				} else {
					return $function->msg_box("Error in processing. Please try again later.",2,"?mod=hstudent");
				}
			} else {
				$testid = intval($_GET['test']);
				$smarty->assign("testid", $testid);
				// check date of test
				$time = $oStudent->getDateTimeLimit($testid);
				$now = date("Y-m-d", time());
				if($now > $time['Deadline']) {
					return $function->msg_box("Deadline is over. You can't do this test anymore.",2,"?mod=student&a=assessment");
				}
				if(is_null($time['UnitID'])) {
					$smarty->assign("time_limit", $time['TimeLimit']);
				} else $smarty->assign("time_limit", 0);
				//-------------------
				$check_module = $oStudent->check_module_test($testid, $_SESSION['user_logged']['UID']); // test of module just can do one time
				if($check_module) {
					return $function->msg_box("You had done this test. Please choose Review Done Assessment to review your answers.",2,"?mod=student&a=amodule");
				}
				$done_test = $oStudent->check_do_test($testid, $_SESSION['user_logged']['UID']);
				//if($done_test['TestID'] == "") {
					//return $function->msg_box("You had done this test. Please choose Review Test to review or edit your answers.",2,"?mod=hstudent");
				//} else {
					if($done_test['Attempt'] != null) {
						$list_questions = $oStudent->getAllQuestionsAnswerInTest($testid, $_SESSION['user_logged']['UID'], $done_test['Attempt']);
						$number_questions = count($list_questions);
						$smarty->assign("list_questions", $list_questions);
						$smarty->assign("attempt", $done_test['Attempt']);
						$smarty->assign("number_questions", $number_questions);
						return $smarty->fetch("student/reviewtest.html");	
					} else {
						$list_questions = $oStudent->getAllQuestionsInTest($testid);
						$number_questions = count($list_questions);
						$smarty->assign("list_questions", $list_questions);
						$smarty->assign("attempt", $done_test['Attempt']);
						$smarty->assign("number_questions", $number_questions);
						return $smarty->fetch("student/dotest.html");
					}
				//}	
			}
		break;
		
		case "ureview":
			$list_done = $oStudent->showAllDoneTest($_SESSION['user_logged']['UID']);
			for($i=0; $i < count($list_done); $i++) {
				$att = $list_done[$i]['Attempt'];
				for($j=1; $j<= $att; $j++) {
					$n_arr[$i][] = $j;
				}
			}
			$smarty->assign("list_done", $list_done);
			$smarty->assign("n_arr", $n_arr);
			$smarty->assign("um", 1); // 1: Unit
			return $smarty->fetch("student/reviewdone.html");
		break;
		
		case "mreview":
			$list_done = $oStudent->showAllDoneTestsInModule($_SESSION['user_logged']['UID']);
			$smarty->assign("list_done", $list_done);
			$smarty->assign("um", 0); // 0: Module
			return $smarty->fetch("student/reviewdone.html");
		break;
		
		case "reviewtest":
			$testid = isset($_GET['test']) ? intval($_GET['test']) : 0;
			$at = isset($_GET['at']) ? intval($_GET['at']) : 0;
			$mark_temp = $oStudent->getAllMarkOfAssessment($testid, $_SESSION['user_logged']['UID']);
			$mark_tmp2 = 0;
			$num_attempt = count($mark_temp);
			for($i=0; $i < $num_attempt; $i++) {
				$mark_tmp2 += (int)$mark_temp[$i]['OverallMark'];
			}
			$overallmark = round($mark_tmp2/$num_attempt, 1); // average mark by tong diem / tong attempt, lam tron len
			$list_questions = $oStudent->getAllQuestionsAnswerInTest($testid, $_SESSION['user_logged']['UID'], $at);
			$number_questions = count($list_questions);
			//$smarty->assign("attempt", $done_test['Attempt']);
			$smarty->assign("number_questions", $number_questions);
			$smarty->assign("testid", $testid);
			$smarty->assign("list_questions", $list_questions);
			$smarty->assign("average_mark", $overallmark);
			return $smarty->fetch("student/reviewtest_detail.html");
		break;
		
		case "viewgrade":
			$m_without_u = $oStudent->getAllModuleWithoutUnit();
			for($i=0; $i < count($m_without_u); $i++) {
				$list[$i] = $oStudent->getAllDoneTestsInModule($_SESSION['user_logged']['UID'], $m_without_u[$i]['ModuleID']);
				$mark_tmp2 = 0;
				$num_attempt = count($list[$i]);
				for($j=0; $j < $num_attempt; $j++) {
					$mark_tmp2 += (int)$list[$i][$j]['OverallMark'];
				}
				$m_without_u[$i]['Averagemark'] = round($mark_tmp2/$num_attempt, 1); // average mark by tong diem / tong attempt, lam tron len*/
			}
			$smarty->assign("m_without_u", $m_without_u);
			$smarty->assign("list_assessment_module", $list);
			return $smarty->fetch("student/viewgrade.html");
		break;
	
	}

}

?>